/*
 *
 * -------------------------------------------------------------------------------------------------
 *                            COPYRIGHT(C) BOCO HEALTH 2016
 * -------------------------------------------------------------------------------------------------
 *
 * =================================================================================================
 *  *HISTORY
 *
 *      Tag                Date            Author           Description
 * =================================================================================================
 *      created            2017/1/17      caodailu        create
 * =================================================================================================
 *
 *
 */

package com.boco.familyuser.view.main;

import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.login.IYWConnectionListener;
import com.boco.familyuser.R;
import com.boco.familyuser.base.BaseActivity;
import com.boco.familyuser.base.BaseFragment;
import com.boco.familyuser.custom.NoSlideViewPager;
import com.boco.familyuser.utils.ActivityCache;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements BaseFragment.BackHandledInterface {

    @BindView(R.id.main_viewPager)
    NoSlideViewPager mMainViewPager;
    @BindView(R.id.main_tabs)
    TabLayout mMainTabs;


    private Fragment mFragment1;
    private Fragment mFragment2;
    private Fragment mFragment3;
    private Fragment mFragment4;

    private Context mContext;

    public static final String SYSTEM_REQ_CONVERSATION = "系统管理员";
    public static final String SYSTEM_FRIEND_REQ_CONVERSATION = "sysfrdreq";
    public static final String START_CONTACTS = "跳转百川联系人";
    public static final String IM_LOGIN_SUCCESS = "百川登录成功";
    private YWIMKit mYWIMKit;

    private IYWConnectionListener mConnectionListener;
    private NotificationManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        contentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        ActivityCache.finishActivity();
        mContext = this;
        initView();
    }

    private void initView() {

        List<Fragment> fragments = new ArrayList<>();
        mFragment1 = new MyFragment();
        mFragment2 = new MyFragment();
        mFragment3 = new MyFragment();
        mFragment4 = new MyFragment();
        fragments.add(mFragment1);
        fragments.add(mFragment2);
        fragments.add(mFragment3);
        fragments.add(mFragment4);

        SimplePagerAdapter mAdapter = new SimplePagerAdapter(this, getSupportFragmentManager(),
                fragments);
        mMainViewPager.setAdapter(mAdapter);

        mMainTabs.addTab(mMainTabs.newTab());
        mMainTabs.addTab(mMainTabs.newTab());
        mMainTabs.addTab(mMainTabs.newTab());
        mMainTabs.addTab(mMainTabs.newTab());
        for (int i = 3; i >= 0; i--) {
            mMainTabs.getTabAt(i).setCustomView(mAdapter.getTabView(i)).select();
        }
        mMainTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView tv = (TextView) tab.getCustomView().findViewById(R.id.home_tab_tv);
                tv.setTextColor(ContextCompat.getColor(mContext, R.color.color_22c064));
                mMainViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                TextView tv = (TextView) tab.getCustomView().findViewById(R.id.home_tab_tv);
                tv.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });



    }

    private BaseFragment mBaseFragment;

    @Override
    public void setSelectedFragment(BaseFragment selectedFragment) {
        mBaseFragment = selectedFragment;
    }

    long currentTime;

    @Override
    public void onBackPressed() {
//        super.onBackPressed();

        if (mBaseFragment == null || !mBaseFragment.onBackPressed()) {
            if (System.currentTimeMillis() - currentTime <= 2000) {
                System.exit(0);
            } else {
                Toast.makeText(this, "再点一次即可退出程序", Toast.LENGTH_SHORT).show();
                currentTime = System.currentTimeMillis();
            }
        }
    }

}
